//
// Created by pzw on 24-2-23.
//

#include "memory_std.h"
#include "../file/file_stdio.h"

void *memory_std::memory_std_malloc(size_t size) { // NOLINT(*-convert-member-functions-to-static)
    return malloc(size);
}

void memory_std::memory_std_free(void *ptr_to_memory) {
    free(ptr_to_memory);
}

#define A_MEGABYTE (1024 * 1024)
#define PHY_MEM_MEGS 1024

int memory_std_simple_main() {
    memory_std memoryStd{};
    file_stdio fileStdio{};
    char *some_memory;
#if 0
    int megabyte = A_MEGABYTE;
    int exit_code = EXIT_FAILURE;

    some_memory = (char *) memoryStd.memory_std_malloc(megabyte);
    if (some_memory != nullptr) {
        fileStdio.file_stdio_sprintf(some_memory, "Hello world!\n");
        fileStdio.file_stdio_printf("%s", some_memory);
        exit_code = EXIT_SUCCESS;
    }

    return exit_code;
#elif 1
    size_t size_to_allocate = A_MEGABYTE;
    int megs_obtained = 0;

    while (megs_obtained < (PHY_MEM_MEGS * 2)) {
        some_memory = (char *) memoryStd.memory_std_malloc(size_to_allocate);
        if (some_memory != nullptr) {
            megs_obtained++;
            fileStdio.file_stdio_sprintf(some_memory, "Hello world!\n");
            fileStdio.file_stdio_printf("%s - now allocated %d Megabytes\n", some_memory, megs_obtained);
        } else {
            return EXIT_FAILURE;
        }
    }
    return EXIT_SUCCESS;
#endif
}